package com.example.gaojt.androiddemo;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.gaojt.androiddemo.CoordinatorLayoutDemo.CoordinatorLayoutDemo;
import com.example.gaojt.androiddemo.cleanDemo.CleanDemoActivity;
import com.example.gaojt.androiddemo.intentCover.IntentToUri;
import com.example.gaojt.androiddemo.jsoupStudy.JsoupStudyActivity;
import com.example.gaojt.androiddemo.statical.DemoActivity;
import com.example.gaojt.androiddemo.webViewStudy.WebViewStudyActivity;

import java.io.File;

public class ScrollingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrolling);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null)
                        .show();
            }
        });
        findViewById(R.id.intent_cover).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ScrollingActivity.this, IntentToUri.class));
            }
        });

        findViewById(R.id.jsoup_study).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ScrollingActivity.this, JsoupStudyActivity.class));
            }
        });

        findViewById(R.id.webView_study).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ScrollingActivity.this, WebViewStudyActivity.class));
            }
        });

        findViewById(R.id.statical_study).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ScrollingActivity.this, DemoActivity.class));
            }
        });

        findViewById(R.id.clean_demo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ScrollingActivity.this, CleanDemoActivity.class));
            }
        });

        findViewById(R.id.coordinator_layout_demo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ScrollingActivity.this, CoordinatorLayoutDemo.class));
            }
        });

        TextView a = (TextView) findViewById(R.id.aaa);

        String s = "默认颜色<font color='#FF0000'>红颜色</font>";
        Log.d("aa", "onCreate: s = " + s);
        Spanned ss = Html.fromHtml(s);
        Log.d("aa", "onCreate: ss = " + ss);
        a.setText(ss);

        long[] sd = getAvailableBytes(Environment.getExternalStorageDirectory().getAbsolutePath());
        if (sd != null) {
            Log.d("Apps", "onCreate: t = " + sd[0] + ",a = " + sd[1]);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_scrolling, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        // noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static final int NO_SPACE_ERROR = -1;

    public static long[] getAvailableBytes(String path) {
        Log.d("Apps", "getAvailableBytes: path = " + path);
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        try {
            File pathFile = new File(path);
            StatFs stat = new StatFs(pathFile.getAbsolutePath());
            if (Build.VERSION.SDK_INT >= 18) {
                long total = stat.getBlockCountLong() * stat.getBlockSizeLong();
                long available = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
                return new long[]{total, available};
            }
            long total = stat.getBlockCount() * stat.getBlockSize();
            long available = stat.getAvailableBlocks() * stat.getBlockSize();
            return new long[]{total, available};
        } catch (Exception e) {
            Log.d("Apps", "Fail to access external storage", e);
        }
        return null;
    }
}
